package com.woniuxy;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 作者
 * @create 2025-09-12 14:08
 */
public class Test02 {
    public static void main(String[] args) {
        test08();
    }

    private static void test08() {
        Book book1 = new Book("1","book1");
        System.out.println(book1);
        Map<String,String> mapbook1=new HashMap();
        mapbook1.put("bid", "2");
        mapbook1.put("bname", "book2");
        System.out.println(mapbook1);
    }

    private static void test07() {
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"aaa");
        map.put(2,"bbb");
        map.put(3,"ccc");
        System.out.println("map遍历方法一：获取所有key的集合");
        //获取所有key的集合
        Set<Integer> keys= map.keySet();
        for(Integer key:keys){
            System.out.println("key="+key+",value="+map.get(key));
        }

        System.out.println("map遍历方法二：获取所有value的集合");
        //Collection
        Collection<String> values = map.values();
        for(String str: values){
            System.out.println(str);
        }

        System.out.println("map遍历方法二：获取所有key-value的集合");
        //entrySet():返回值Set<Map.Entry<Integer, String>>
        //Map.Entry<K, v>:Entry是Map接口中定义的一个接口，代表Key-Value
        //返回一个Set集合，集合中的元素类型是  Map.Entry<Integer, String>
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for(Map.Entry<Integer, String>   entry :entries ){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
    }

    private static void test06() {
        Map<String, Book> map = new HashMap<>();
        map.put("001", new Book("001", "aaa"));
        map.put("002", new Book("002", "bbb"));
        System.out.println(map.containsKey("001"));
        System.out.println(map.containsKey("003"));
        System.out.println(map.containsValue(new Book("001", "aaa")));
        System.out.println(map.containsValue(new Book("001", "xxx")));
        System.out.println(map);
        map.remove("001");
        System.out.println(map);
    }

    private static void test05() {
        Map<Book, String> map = new HashMap<>();
        map.put(new Book("001", "aaa"), "001");
        map.put(new Book("002", "bbb"), "002");
        map.put(new Book("002", "bbb"), "003");
        System.out.println(map);
    }

    private static void test04() {
        Map<String, Book> map = new HashMap<>();
        map.put("001", new Book("001", "aaa"));
        map.put("002", new Book("002", "bbb"));
        map.put("001", new Book("003", "ccc"));
        System.out.println(map.get("001"));
        System.out.println(map);
    }

    private static void test02() {
        Map<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 3);
        System.out.println(map.get("1"));
        System.out.println(map.get("a"));
        System.out.println(map);
    }

    private static void test01() {
        //定义一个map，使用Integer作为key的类型，String做值的类型
        Map<Integer, String> map = new HashMap<Integer, String>();
        System.out.println(map.put(1, "zs"));
        map.put(2, "ls");
        //通过key获取value
        String name = map.get(1);
        System.out.println(name);
        System.out.println(map);
        //添加同名的key，value覆盖
        System.out.println(map.put(1, "zxs"));
        System.out.println(map);
    }
}
